package com.hsu.apibackend.service.inner.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hsu.apibackend.common.ErrorCode;
import com.hsu.apibackend.exception.BusinessException;
import com.hsu.apibackend.model.entity.User;
import com.hsu.apibackend.service.UserService;
import com.hsu.apicommon.model.vo.UserVO;
import com.hsu.apicommon.service.inner.InnerUserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;

import javax.annotation.Resource;

/**
 * @Author: Hsu琛君珩
 * @Date: 2024年09月09日 19:44
 * @Version: v1.0.0
 * @Description: 实现根据 AccessKey 查询用户的服务。
 */
@DubboService
public class InnerUserServiceImpl implements InnerUserService {

    @Resource
    private UserService userService;

    /**
     * 根据 AccessKey 查询用户信息
     *
     * @param accessKey 用户的访问密钥
     * @return 返回用户的详细信息，如果用户不存在则返回 null
     */
    @Override
    public UserVO getInvokeUserByAccessKey(String accessKey) {
        // 检查 AccessKey 是否为空
        if (StringUtils.isAnyBlank(accessKey)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        // 构造查询条件
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(User::getAccessKey, accessKey);

        // 根据 AccessKey 查询用户
        User user = userService.getOne(userLambdaQueryWrapper);
        if (user == null) {
            return null;
        }

        // 将 User 对象的属性拷贝到 UserVO 对象
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(user, userVO);
        return userVO;
    }
}
